function loadJS(url, callback = function() {}) {
	return new Promise((success) => {
		const script = document.createElement('script')
		script.type = 'text/javascript';
		//IE
		if (script.readyState) {
			script.onreadystatechange = function() {
				if (script.readyState == 'loaded' || script.readyState == 'complete') {
					script.onreadystatechange = null;
					callback();
				}
			};
		} else {
			//其他浏览器
			script.onload = function() {
				callback();
				success();
			};
		}
		script.src = url;
		document.getElementsByTagName('body')[0].appendChild(script);
	});
}

/**
 * 动态加载CSS
 * @param {string} url 样式地址
 */
function loadCss(url) {
	var head = document.getElementsByTagName('head')[0];
	var link = document.createElement('link');
	link.type = 'text/css';
	link.rel = 'stylesheet';
	link.href = url;
	head.appendChild(link);
}

function loadCssList(urls = []) {
	urls.forEach(item => loadCss(item));
}

function loadJSList(urls = [], call = function() {}) {
	let index = 0;
	let callLength = urls.length;
	return new Promise(async (success) => {
		function callback() {
			if (--callLength === 0) {
				call();
				success();
			}
		}
		for (var i = 0; i < urls.length; i++) {
			await loadJS(urls[i], callback);
		}
	});
}

export default {
	loadJS,
	loadJSList,
	loadCss,
	loadCssList
}
